//
//  NewsTableViewCell.m
//  BasketballClub
//
//  Created by David on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "NewsTableViewCell.h"
#import <UIImageView+WebCache.h>
@interface NewsTableViewCell()
- (NSString *)transTime:(NSString *)date;
@end
@implementation NewsTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self allViews];
    }
    return self;
}

-(void)allViews
{
    
    //图片
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5 *LAYOUT_SIZE, 0*LAYOUT_SIZE_HEIGHT, 404*LAYOUT_SIZE, 250*LAYOUT_SIZE_HEIGHT)];
    [self addSubview:self.imgView];
    
    //遮挡
    self.dangView = [[UILabel alloc] initWithFrame:CGRectMake(-1*LAYOUT_SIZE, 240*LAYOUT_SIZE_HEIGHT, 406*LAYOUT_SIZE, 11*LAYOUT_SIZE_HEIGHT)];
    self.dangView.backgroundColor = [UIColor whiteColor];
    [self.imgView addSubview:self.dangView];
    
    //title
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5*LAYOUT_SIZE, 250*LAYOUT_SIZE_HEIGHT , 404*LAYOUT_SIZE, 30*LAYOUT_SIZE_HEIGHT)];
    self.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:(20*LAYOUT_SIZE)];
    [self addSubview:self.titleLabel];
    
    //内容
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(5*LAYOUT_SIZE, 280*LAYOUT_SIZE_HEIGHT, 404*LAYOUT_SIZE, 90*LAYOUT_SIZE_HEIGHT)];
    self.contentLabel.font = [UIFont systemFontOfSize:16*LAYOUT_SIZE];
    self.contentLabel.textColor = [UIColor grayColor];
    [self addSubview:self.contentLabel];
    
    //距离时间
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5*LAYOUT_SIZE, 375*LAYOUT_SIZE_HEIGHT, 60*LAYOUT_SIZE, 20*LAYOUT_SIZE_HEIGHT)];
    self.timeLabel.textColor = [UIColor lightGrayColor];
    self.timeLabel.font = [UIFont systemFontOfSize:14*LAYOUT_SIZE];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.timeLabel];
    
    //阅读数据
    self.readLabel = [[UILabel alloc] initWithFrame:CGRectMake(70*LAYOUT_SIZE, 375*LAYOUT_SIZE_HEIGHT, 30*LAYOUT_SIZE, 20*LAYOUT_SIZE_HEIGHT)];
    self.readLabel.textColor = [UIColor lightGrayColor];
    self.readLabel.font = [UIFont systemFontOfSize:14*LAYOUT_SIZE];
    self.readLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.readLabel];
    
    //固定"人阅读"字样
    self.peopleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100*LAYOUT_SIZE, 375*LAYOUT_SIZE_HEIGHT, 45*LAYOUT_SIZE, 20*LAYOUT_SIZE_HEIGHT)];
    self.peopleLabel.textColor = [UIColor lightGrayColor];
    self.peopleLabel.text = @"人阅读";
    self.peopleLabel.font = [UIFont systemFontOfSize:14*LAYOUT_SIZE];
    self.peopleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.peopleLabel];
    
    
    //喜欢
    self.likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(155*LAYOUT_SIZE, 375*LAYOUT_SIZE_HEIGHT, 60*LAYOUT_SIZE, 20*LAYOUT_SIZE_HEIGHT)];
    self.likeLabel.textColor = [UIColor lightGrayColor];
    self.likeLabel.font = [UIFont systemFontOfSize:14*LAYOUT_SIZE];
    self.likeLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.likeLabel];
    
    //新浪体育
    self.authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(329*LAYOUT_SIZE, 375*LAYOUT_SIZE_HEIGHT, 80*LAYOUT_SIZE, 20*LAYOUT_SIZE_HEIGHT)];
    self.authorLabel.font = [UIFont systemFontOfSize:14*LAYOUT_SIZE];
    self.authorLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:self.authorLabel];
}

-(void)setNewsModel:(NewsModel *)newsModel
{
    //图片
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:newsModel.imglink]];
    //title
    self.titleLabel.text = newsModel.title;
    //内容
    self.contentLabel.text = newsModel.content168;
    self.contentLabel.numberOfLines = 0;
    //新浪体育
    self.authorLabel.text = newsModel.author;
    //阅读
//    self.readLabel.text = [NSString stringWithFormat:@"%@",newsModel.readarts];
    //喜欢
    self.likeLabel.text = [NSString stringWithFormat:@"%@喜欢",newsModel.likecount];
    //距离时间
    self.timeLabel.text = [self transTime:newsModel.timeSecond];
}


#pragma mark - 换算时间方法
- (NSString *)transTime:(NSString *)date
{
    NSDate *dateNow = [NSDate date];
    NSInteger duration = (NSInteger)[dateNow timeIntervalSince1970] - [date integerValue];
    NSString *string = [NSString new];
    int second = 1;
    int minute = second * 60;
    int hour = minute * 60;
    int day = hour * 24;
    int week = day * 7;
    int month = day * 31;
    if (duration <= 7 * second) {
        string = @"刚刚发布";
    }else if (duration < minute) {
        int n = (int)duration/second;
        string = [NSString stringWithFormat:@"%d秒钟前",n];
    }else if (duration < hour){
        int n = (int)duration/minute;
        string = [NSString stringWithFormat:@"%d分钟前",n];
    }else if (duration < day){
        int n = (int)duration/hour;
        string = [NSString stringWithFormat:@"%d小时前",n];
    }else if (duration < week){
        int n = (int)duration/day;
        string = [NSString stringWithFormat:@"%d天以前",n];
    }else if (duration < month){
        int n = (int)duration/week;
        string = [NSString stringWithFormat:@"%d周以前",n];
    }else if (duration > month){
        string = @"很久以前";
    }
    return string;
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
